package jsyf.oa.dao.tmpevent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.entity.tmpEvent.EventMainEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

@Repository
public class EventMainDao extends BaseDao {

    public EventMainDao() {
        super.iBaseEntity = new EventMainEntity();
    }

    public List<EventMainEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<EventMainEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            EventMainEntity entity = (EventMainEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    public EventMainEntity getEntityById(String id) {
        return (EventMainEntity) super.getIBaseEntityById(id);
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, EventMainEntity.class);
    }

    public List<JSONObject> getJSONListByPage(String currentUserId, String condition, Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ");
        sql.append("ID AS id, ");
        sql.append("TYPE AS type, ");
        sql.append("TITLE AS title, ");
        sql.append("ORG_NAME AS orgName, ");
        sql.append("REGION_NAME AS regionName, ");
        sql.append("DESCRIPTION AS description, ");
        sql.append("CREATE_USER_ID AS createUserId, ");
        sql.append("CREATE_TIME AS createTime,");
        //sql.append("DATE_FORMAT(CREATE_TIME, '%H:%i %m-%d') AS createTime, ");
        sql.append("AUDIT_FLAG AS auditFlag, ");
        sql.append("CREATE_USER_NAME AS createUserName, ");
        sql.append("CURRENT_AUDITOR_ID AS currentAuditorId, ");
        sql.append("CURRENT_AUDITOR_NAME AS currentAuditorName, ");
        sql.append("COMPANY_ID AS companyId ");
        sql.append(" FROM T_TMP_EVENT_MAIN ");
        sql.append(" WHERE 1 = 1 ");
        sql.append(condition);
        sql.append("  ORDER BY CREATE_TIME desc ");

        super.appendPageSql(sql, requestPage, pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);

        return jsonList;
    }

    public Integer getUnDoCount(String userId) {
        StringBuffer sql = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<>();
        sql.append(" SELECT COUNT(1) AS count FROM " + iBaseEntity.getTableName() + " WHERE CURRENT_AUDITOR_ID = ? ");
        sqlArgs.add(userId);
        String count = super.queryForString(sql.toString(), sqlArgs.toArray());
        return Integer.parseInt(count);
    }

    public Integer copierReturn(String id, String copierReturnReason) {
        JSONObject obj = new JSONObject();
        obj.put("ID", id);
        obj.put("COPIER_RETURN_REASON", copierReturnReason);
        obj.put("AUDIT_FLAG", "2");
        obj.put("CURRENT_AUDITOR_ID", "");
        obj.put("CURRENT_AUDITOR_NAME", "");
        JSONObject objId = new JSONObject();
        objId.put("ID", id);
        return super.update("T_TMP_EVENT_MAIN", obj, objId);
    }
}

